package jwei.apps.indextank.db;

import java.util.ArrayList;
import java.util.List;

import jwei.apps.indextank.db.RestaurantCache.RestaurantsCache;
import jwei.apps.indextank.objects.Restaurant;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

public class RestaurantDb {

    private RestaurantDb() {
    }

    private static final RestaurantDb instance = new RestaurantDb();

    public static RestaurantDb getInstance() {
        return instance;
    }

    public void addProduct(ContentResolver contentResolver, Restaurant r) {
        if (!isCached(contentResolver, r.getDocid())) {
            ContentValues contentValue = new ContentValues();
            contentValue.put(RestaurantsCache.DOC_ID, r.getDocid());
            contentValue.put(RestaurantsCache.NAME, r.getName());
            contentValue.put(RestaurantsCache.STREET, r.getStreet());
            contentValue.put(RestaurantsCache.CITY, r.getCity());
            contentValue.put(RestaurantsCache.STATE, r.getState());
            contentValue.put(RestaurantsCache.ZIP, r.getZip());
            contentValue.put(RestaurantsCache.PHONE, r.getPhone());
            contentValue.put(RestaurantsCache.WEBSITE, r.getUrl());
            contentValue.put(RestaurantsCache.TAGS, r.getTags());
            contentValue.put(RestaurantsCache.DISHES, r.getDishes());
            contentValue.put(RestaurantsCache.INGREDIENTS, r.getIngredients());
            contentValue.put(RestaurantsCache.IS_QUEUE, 0);
            contentResolver.insert(RestaurantsCache.CONTENT_URI, contentValue);
        }
    }

    public int addProducts(ContentResolver contentResolver, List<Restaurant> restaurants) {
        List<ContentValues> contentValues = new ArrayList<ContentValues>();
        for (Restaurant r : restaurants) {
            if (!isCached(contentResolver, r.getDocid())) {
                ContentValues contentValue = new ContentValues();
                contentValue.put(RestaurantsCache.DOC_ID, r.getDocid());
                contentValue.put(RestaurantsCache.NAME, r.getName());
                contentValue.put(RestaurantsCache.STREET, r.getStreet());
                contentValue.put(RestaurantsCache.CITY, r.getCity());
                contentValue.put(RestaurantsCache.STATE, r.getState());
                contentValue.put(RestaurantsCache.ZIP, r.getZip());
                contentValue.put(RestaurantsCache.PHONE, r.getPhone());
                contentValue.put(RestaurantsCache.WEBSITE, r.getUrl());
                contentValue.put(RestaurantsCache.TAGS, r.getTags());
                contentValue.put(RestaurantsCache.DISHES, r.getDishes());
                contentValue.put(RestaurantsCache.INGREDIENTS, r.getIngredients());
                contentValue.put(RestaurantsCache.IS_QUEUE, 0);
                contentValues.add(contentValue);
            }
        }
        int rows = contentResolver
                .bulkInsert(RestaurantsCache.CONTENT_URI, contentValues.toArray(new ContentValues[0]));
        return rows;
    }

    public void updateRestaurantQueue(ContentResolver contentResolver, String docid, int status) {
        ContentValues contentValue = new ContentValues();
        contentValue.put(RestaurantsCache.IS_QUEUE, status);
        contentResolver.update(RestaurantsCache.CONTENT_URI, contentValue,
                RestaurantsCache.DOC_ID + "='" + docid + "'", null);
    }

    public void updateRestaurantTags(ContentResolver contentResolver, String docid, String tags) {
        ContentValues contentValue = new ContentValues();
        contentValue.put(RestaurantsCache.TAGS, tags);
        contentResolver.update(RestaurantsCache.CONTENT_URI, contentValue,
                RestaurantsCache.DOC_ID + "='" + docid + "'", null);
    }

    public void updateRestaurantDishes(ContentResolver contentResolver, String docid, String dishes) {
        ContentValues contentValue = new ContentValues();
        contentValue.put(RestaurantsCache.DISHES, dishes);
        contentResolver.update(RestaurantsCache.CONTENT_URI, contentValue,
                RestaurantsCache.DOC_ID + "='" + docid + "'", null);
    }

    public void updateRestaurantIngredients(ContentResolver contentResolver, String docid, String ingredients) {
        ContentValues contentValue = new ContentValues();
        contentValue.put(RestaurantsCache.INGREDIENTS, ingredients);
        contentResolver.update(RestaurantsCache.CONTENT_URI, contentValue,
                RestaurantsCache.DOC_ID + "='" + docid + "'", null);
    }

    public Restaurant getRestaurant(ContentResolver contentResolver, String docid) {
        Restaurant ret = null;
        Cursor c = contentResolver.query(RestaurantsCache.CONTENT_URI, null, RestaurantsCache.DOC_ID + "='" + docid
                + "'", null, null);
        if (c == null) { return null; }
        if (c.moveToNext()) {
            String name = c.getString(c.getColumnIndex(RestaurantsCache.NAME));
            ret = new Restaurant(docid, name);
            ret.setStreet(c.getString(c.getColumnIndex(RestaurantsCache.STREET)));
            ret.setCity(c.getString(c.getColumnIndex(RestaurantsCache.CITY)));
            ret.setState(c.getString(c.getColumnIndex(RestaurantsCache.STATE)));
            ret.setZip(c.getString(c.getColumnIndex(RestaurantsCache.ZIP)));
            ret.setPhone(c.getString(c.getColumnIndex(RestaurantsCache.PHONE)));
            ret.setUrl(c.getString(c.getColumnIndex(RestaurantsCache.WEBSITE)));
            ret.setTags(c.getString(c.getColumnIndex(RestaurantsCache.TAGS)));
            ret.setDishes(c.getString(c.getColumnIndex(RestaurantsCache.DISHES)));
            ret.setIngredients(c.getString(c.getColumnIndex(RestaurantsCache.INGREDIENTS)));
        }
        c.close();
        return ret;
    }

    public int getRestaurantQueueStatus(ContentResolver contentResolver, String docid) {
        Cursor c = contentResolver.query(RestaurantsCache.CONTENT_URI, new String[] { RestaurantsCache.IS_QUEUE },
                RestaurantsCache.DOC_ID + "='" + docid + "'", null, null);
        if (c == null) { return -1; }
        if (c.moveToNext()) {
            int status = c.getInt(c.getColumnIndex(RestaurantsCache.IS_QUEUE));
            return status;
        }
        c.close();
        return -1;
    }

    public boolean isCached(ContentResolver contentResolver, String docid) {
        boolean ret = false;
        Cursor c = contentResolver.query(RestaurantsCache.CONTENT_URI, new String[] { RestaurantsCache._ID },
                RestaurantsCache.DOC_ID + "='" + docid + "'", null, null);
        if (c == null) { return ret; }
        if (c.moveToNext()) {
            ret = true;
        }
        c.close();
        return ret;
    }

    public boolean isEmpty(ContentResolver contentResolver) {
        boolean ret = true;
        String[] projection = new String[] { RestaurantsCache._ID };
        Cursor c = contentResolver.query(RestaurantsCache.CONTENT_URI, projection, null, null, null);
        if (c == null) { return ret; }
        if (c.moveToNext()) {
            ret = true;
        }
        c.close();
        return ret;
    }

    // delete all non-bookmarked
    public void refreshCache(ContentResolver contentResolver) {
        contentResolver.delete(RestaurantsCache.CONTENT_URI, RestaurantsCache.IS_QUEUE + "=0", null);
    }

}
